#include "narrationwidget.h"
NarrationWidget::NarrationWidget(QWidget* parent)
{
    Init(parent);
}

NarrationWidget::~NarrationWidget()
{
    delete edit;
}

void NarrationWidget::LoadTree(NarrationNode *root,int curRowId)
{
    if(!root) return;
    //释放Widget内存
    for(int i = 0;i < this->count();i++)
    {
        QListWidgetItem* item = this->item(i);
        QWidget* w = this->itemWidget(item);
        this->removeItemWidget(item);
        delete w;
    }
    this->clear();

    while(root)
    {
        QListWidgetItem *item = new QListWidgetItem();
        item->setSizeHint(QSize(450,200));
        NarrationWidgetItem *itemWidget = new NarrationWidgetItem(this,root);
        this->addItem(item);
        this->setItemWidget(item,itemWidget);
        if(root->id == curRowId)
            this->setCurrentItem(item);
        root = NarrationTree::Instance().narrationTree[root->childId];
    }
    if(!curRowId)
        this->setCurrentRow(0);
}

void NarrationWidget::ShowBranchs(NarrationNode *node)
{
    //Linker
    bool hasOption = false;
    for(int i = 0;i < node->linkers.count(); i++)
    {
        if(node->linkers.at(i)->branchId >= 0)
        {
            hasOption = true;
            break;
        }
    }
    if(!hasOption)
        return;
    this->clear();
    for(int i = 0;i < node->linkers.count(); i++)
    {
        if(node->linkers.at(i)->branchId >= 0)
        {
            QListWidgetItem* item = new QListWidgetItem();
            item->setText("[剧情分支]" + node->linkers.at(i)->name);
            item->setIcon(QIcon(":/res/icon/redo.png"));
            this->addItem(item);
        }
    }
}

void NarrationWidget::Init(QWidget *parent)
{
    LoadTree(GameManager::Instance().curNode);
    connect(&EventCenter::Instance(),&EventCenter::ShowNarrationItem,this,[=](int id){
        //TODO编辑模式，显示节点
        this->clear();
    });
    connect(this,&QListWidget::currentItemChanged,this,[&](QListWidgetItem* item){
        if(!item)
            return;
        QWidget* w = QListWidget::itemWidget(item);
        NarrationWidgetItem* nw = dynamic_cast<NarrationWidgetItem*>(w);
        if(nw && nw->node)
            GameManager::Instance().ShowNarration(nw->node);
    });
    connect(this,&QListWidget::currentRowChanged,this,[&](int row){
        ChangeCurNode(row);
    });

    connect(this,&QListWidget::itemDoubleClicked,this,[=](QListWidgetItem* item){
        QWidget* w = QListWidget::itemWidget(item);
        NarrationWidgetItem* nw = dynamic_cast<NarrationWidgetItem*>(w);
        if(nw && nw->node)
        {
            ShowBranchs(nw->node);
        }
    });
    connect(&EventCenter::Instance(),&EventCenter::AddNode,this,&NarrationWidget::AddNode);
    connect(&EventCenter::Instance(),&EventCenter::TextEdit,this,[=](QString& name,QString& text){
        QWidget* w = QListWidget::itemWidget(this->currentItem());
        NarrationWidgetItem* nw = dynamic_cast<NarrationWidgetItem*>(w);
        nw->LoadNode();
    });
    connect(&EventCenter::Instance(),&EventCenter::RefreshCurNodeItem,this,[=](){
        QWidget* w = QListWidget::itemWidget(this->currentItem());
        NarrationWidgetItem* nw = dynamic_cast<NarrationWidgetItem*>(w);
        nw->LoadNode();
    });
    edit = new QLineEdit();
    edit->setWindowTitle("修改节点备注");
    edit->setWindowIcon(QIcon(":/res/icon/myGame.png"));
    edit->setFixedSize(400,50);
    edit->setWindowModality(Qt::ApplicationModal);
    connect(edit,&QLineEdit::editingFinished,this,[=](){
        QWidget* w = QListWidget::itemWidget(this->currentItem());
        NarrationWidgetItem* nw = dynamic_cast<NarrationWidgetItem*>(w);
        if(nw)
        {
            nw->node->nodeName = edit->text();
            nw->SetName(edit->text());
            emit EventCenter::Instance().Modify();
        }
    });
    if(parent)
        this->setParent(parent);
}

void NarrationWidget::Back()
{
    QWidget* w = QListWidget::itemWidget(this->item(0));
    NarrationNode* node = nullptr;
    if(!w)
    {
        //分支页
        node = GameManager::Instance().curNode;
    }
    else
    {
        NarrationWidgetItem* nw = dynamic_cast<NarrationWidgetItem*>(w);
        if(nw->node->parentId < 0) return;
        node = NarrationTree::Instance().narrationTree[nw->node->parentId];
    }
    NarrationNode* root = node;
    while(root->parentId >= 0 &&
          NarrationTree::Instance().narrationTree[root->parentId]->childId == root->id)
    {
        root = NarrationTree::Instance().narrationTree[root->parentId];
    }
    LoadTree(root,node->id);
}

void NarrationWidget::ShowEdit()
{
    QWidget* w = QListWidget::itemWidget(this->currentItem());
    NarrationWidgetItem* nw = dynamic_cast<NarrationWidgetItem*>(w);
    if(nw)
    {
        edit->setText(nw->node->nodeName);
        edit->show();
    }
}

void NarrationWidget::ChangeCurNode(int row)
{
    QListWidgetItem* item = this->item(row);
    if(!item)
        return;
    QWidget* w = (QListWidget::itemWidget(item));
    if(!w)
    {
        NarrationNode* node = GameManager::Instance().curNode;
        if(!node) return;
        //linkers
        int count = 0;
        for (int i = 0;i < node->linkers.count();i++)
        {
            if(node->linkers.at(i)->branchId >= 0)
            {
                if(count == row)
                {
                    LoadTree(NarrationTree::Instance().narrationTree[node->linkers.at(i)->branchId]);
                    break;
                }
                count++;
            }
        }
    }
    emit EventCenter::Instance().ChangeCurNode();
}

void NarrationWidget::keyPressEvent(QKeyEvent *event)
{
    QListWidget::keyPressEvent(event);
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_C)
    {
        copyId = GameManager::Instance().curNode->id;
    }
    else if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_V)
    {
        NarrationNode* old = NarrationTree::Instance().narrationTree[copyId];
        if(!old)
            return;
        NarrationNode* node = new NarrationNode(*old);
        if(GameManager::Instance().curNode)
        {
            if(GameManager::Instance().curNode->childId >= 0)
            {
                NarrationNode* child = NarrationTree::Instance().narrationTree[GameManager::Instance().curNode->childId];
                child->parentId = node->id;
                node->childId = child->id;
            }
            node->parentId = GameManager::Instance().curNode->id;
            GameManager::Instance().curNode->childId = node->id;
        }
        AddNode(node->id);
    }
}
void NarrationWidget::AddNode(int id)
{
    NarrationNode* node = NarrationTree::Instance().narrationTree[id];
    int curRow = this->currentRow();
    if(!node) return;
    QListWidgetItem *item = new QListWidgetItem();
    item->setSizeHint(QSize(450,200));
    NarrationWidgetItem *itemWidget = new NarrationWidgetItem(this,node);
    this->insertItem(curRow + 1,item);
    this->setItemWidget(item,itemWidget);
    this->setCurrentItem(item);
    emit EventCenter::Instance().Modify();
}

void NarrationWidget::DeleteNodeItem(int row)
{
    QListWidgetItem* i = this->item(row);
    QWidget* w = this->itemWidget(i);
    NarrationWidgetItem* nwi = dynamic_cast<NarrationWidgetItem*>(w);
    if(nwi->node->parentId == -1 || !NarrationTree::Instance().narrationTree.contains(nwi->node->parentId))
        return;
    if(nwi && this->count() > 1)
    {
        this->removeItemWidget(i);
        this->takeItem(row);
        NarrationTree::Instance().DeleteNode(nwi->node->id);
        delete nwi;
        delete i;
    }
    if(nwi && this->count() == 1)
    {
        int id = nwi->node->id;
        Back();
        NarrationTree::Instance().DeleteNode(id);
    }
}
